package com.greenold.Recharge.controller;

import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.greenold.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.greenold.common.annotation.Log;
import com.greenold.common.core.controller.BaseController;
import com.greenold.common.core.domain.AjaxResult;
import com.greenold.common.enums.BusinessType;
import com.greenold.Recharge.domain.Recharge;
import com.greenold.Recharge.service.IRechargeService;
import com.greenold.common.utils.poi.ExcelUtil;
import com.greenold.common.core.page.TableDataInfo;

/**
 * 预交费充值Controller
 * 
 * @author ruoyi
 * @date 2025-10-13
 */
@RestController
@RequestMapping("/recharge/recharge")
public class RechargeController extends BaseController
{
    @Autowired
    private IRechargeService rechargeService;
    /**
     * 根据老人id查询床位
     */
//    @PreAuthorize("@ss.hasPermi('recharge:recharge:query')")
    @GetMapping("/bedNo/{elderId}")
    public AjaxResult getBedNoByElderId(@PathVariable("elderId") String elderId)
    {
        return success(rechargeService.selectBedNoByElderId(elderId));
    }

    /**
     * 查询预交费充值列表
     */
    @PreAuthorize("@ss.hasPermi('recharge:recharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(Recharge recharge)
    {
        startPage();
        List<Recharge> list = rechargeService.selectRechargeList(recharge);
        return getDataTable(list);
    }

    /**
     * 导出预交费充值列表
     */
    @PreAuthorize("@ss.hasPermi('recharge:recharge:export')")
    @Log(title = "预交费充值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Recharge recharge)
    {
        List<Recharge> list = rechargeService.selectRechargeList(recharge);
        ExcelUtil<Recharge> util = new ExcelUtil<Recharge>(Recharge.class);
        util.exportExcel(response, list, "预交费充值数据");
    }

    /**
     * 获取预交费充值详细信息
     */
    @PreAuthorize("@ss.hasPermi('recharge:recharge:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(rechargeService.selectRechargeById(id));
    }

    /**
     * 新增预交费充值
     */
    @PreAuthorize("@ss.hasPermi('recharge:recharge:add')")
    @Log(title = "预交费充值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Recharge recharge)
    {
        String UUId= UUID.randomUUID().toString().replaceAll("-", "").substring(0, 14);
        recharge.setPrepaidNo(UUId);
        String userName = SecurityUtils.getUsername();
        recharge.setCreatedBy(userName);
        return toAjax(rechargeService.insertRecharge(recharge));
    }

    /**
     * 修改预交费充值
     */
    @PreAuthorize("@ss.hasPermi('recharge:recharge:edit')")
    @Log(title = "预交费充值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Recharge recharge)
    {
        return toAjax(rechargeService.updateRecharge(recharge));
    }

    /**
     * 删除预交费充值
     */
    @PreAuthorize("@ss.hasPermi('recharge:recharge:remove')")
    @Log(title = "预交费充值", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(rechargeService.deleteRechargeByIds(ids));
    }
}
